package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ActivitySummary;
import com.ruoyi.system.domain.MeetingSummary;
import com.ruoyi.system.mapper.ActivitySummaryMapper;
import com.ruoyi.system.mapper.MeetingSummaryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CarouselImageMapper;
import com.ruoyi.system.domain.CarouselImage;
import com.ruoyi.system.service.ICarouselImageService;
import org.springframework.util.ObjectUtils;

/**
 * 轮播图管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-25
 */
@Service
public class CarouselImageServiceImpl implements ICarouselImageService
{
    @Autowired
    private CarouselImageMapper carouselImageMapper;
    @Autowired
    private MeetingSummaryMapper meetingSummaryMapper;
    @Autowired
    private ActivitySummaryMapper activitySummaryMapper;

    /**
     * 查询轮播图管理
     *
     * @param id 轮播图管理主键
     * @return 轮播图管理
     */
    @Override
    public CarouselImage selectCarouselImageById(Long id)
    {
        CarouselImage carouselImage = carouselImageMapper.selectCarouselImageById(id);
        String carouselType = carouselImage.getCarouselType();
        if("0".equals(carouselType)){
            Long associationTypeId = carouselImage.getAssociationTypeId();
            if(!ObjectUtils.isEmpty(associationTypeId)){
                MeetingSummary summary = meetingSummaryMapper.selectMeetingSummaryByMeetingId(associationTypeId);
                if(!ObjectUtils.isEmpty(summary)){
                    String summaryImage = summary.getSummaryImage();
                    if(StringUtils.isNotBlank(summaryImage)){
                        List<String> summaryImageList = Arrays.asList(summaryImage.split(","));
                        summary.setSummaryImageList(summaryImageList);
                    }
                }
                carouselImage.setMeetingSummary(summary);
            }
        }else if("1".equals(carouselType)){
            Long associationTypeId = carouselImage.getAssociationTypeId();
            if(!ObjectUtils.isEmpty(associationTypeId)){
                ActivitySummary summary = activitySummaryMapper.selectActivitySummaryByActivityId(associationTypeId);
                if(!ObjectUtils.isEmpty(summary)){
                    String summaryImage = summary.getSummaryImage();
                    if(StringUtils.isNotBlank(summaryImage)){
                        List<String> summaryImageList = Arrays.asList(summaryImage.split(","));
                        summary.setSummaryImageList(summaryImageList);
                    }
                }
                carouselImage.setActivitySummary(summary);
            }
        }
        return carouselImage;
    }

    /**
     * 查询轮播图管理列表
     *
     * @param carouselImage 轮播图管理
     * @return 轮播图管理
     */
    @Override
    public List<CarouselImage> selectCarouselImageList(CarouselImage carouselImage)
    {
        List<CarouselImage> carouselImageList = carouselImageMapper.selectCarouselImageList(carouselImage);
        for (CarouselImage carouselImageVo : carouselImageList){
            String carouselType = carouselImageVo.getCarouselType();
            if("0".equals(carouselType)){
                Long associationTypeId = carouselImageVo.getAssociationTypeId();
                if(!ObjectUtils.isEmpty(associationTypeId)){
                    MeetingSummary summary = meetingSummaryMapper.selectMeetingSummaryByMeetingId(associationTypeId);
                    if(!ObjectUtils.isEmpty(summary)){
                        String summaryImage = summary.getSummaryImage();
                        if(StringUtils.isNotBlank(summaryImage)){
                            List<String> summaryImageList = Arrays.asList(summaryImage.split(","));
                            summary.setSummaryImageList(summaryImageList);
                        }
                    }
                    carouselImageVo.setMeetingSummary(summary);
                }
            }else if("1".equals(carouselType)){
                Long associationTypeId = carouselImageVo.getAssociationTypeId();
                if(!ObjectUtils.isEmpty(associationTypeId)){
                    ActivitySummary summary = activitySummaryMapper.selectActivitySummaryByActivityId(associationTypeId);
                    if(!ObjectUtils.isEmpty(summary)){
                        String summaryImage = summary.getSummaryImage();
                        if(StringUtils.isNotBlank(summaryImage)){
                            List<String> summaryImageList = Arrays.asList(summaryImage.split(","));
                            summary.setSummaryImageList(summaryImageList);
                        }
                    }
                    carouselImageVo.setActivitySummary(summary);
                }
            }
        }
        return carouselImageList;
    }

    /**
     * 新增轮播图管理
     *
     * @param carouselImage 轮播图管理
     * @return 结果
     */
    @Override
    public AjaxResult insertCarouselImage(CarouselImage carouselImage)
    {
        //查询轮播序号是否存在
        String carouselSort = carouselImage.getCarouselSort();
        if(StringUtils.isNotBlank(carouselSort)){
            CarouselImage image = carouselImageMapper.selectCarouselImageBySort(carouselSort);
            if(!ObjectUtils.isEmpty(image)){
                return AjaxResult.error("序号不能重复");
            }
        }
        carouselImage.setCreateTime(DateUtils.getNowDate());
        carouselImageMapper.insertCarouselImage(carouselImage);
        return AjaxResult.success("新增成功");
    }

    /**
     * 修改轮播图管理
     *
     * @param carouselImage 轮播图管理
     * @return 结果
     */
    @Override
    public AjaxResult updateCarouselImage(CarouselImage carouselImage)
    {
        carouselImage.setUpdateTime(DateUtils.getNowDate());
        //查询轮播序号是否存在
        String carouselSort = carouselImage.getCarouselSort();
        CarouselImage carouselImageById = carouselImageMapper.selectCarouselImageById(carouselImage.getId());
        String sort = carouselImageById.getCarouselSort();
        if(StringUtils.isNotBlank(carouselSort)&&!carouselSort.equals(sort)){
            CarouselImage image = carouselImageMapper.selectCarouselImageBySort(carouselSort);
            if(!ObjectUtils.isEmpty(image)){
                return AjaxResult.error("序号不能重复");
            }
        }
        carouselImageMapper.updateCarouselImage(carouselImage);
        return AjaxResult.success("修改成功");
    }

    /**
     * 删除轮播图管理信息
     *
     * @param id 轮播图管理主键
     * @return 结果
     */
    @Override
    public int deleteCarouselImageById(Long id)
    {
        return carouselImageMapper.deleteCarouselImageById(id);
    }
}
